<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    /* 使用工厂函数*/
    //创建饮水机对象，饮水机对象有热水和冷水方法，以及饮水机位置属性
    function dispenser(hot, cold, location) {
      var obj = new Object();
      obj.hot = hot;
      obj.cold = cold;
      obj.location = location;
      return obj;
    }
    var dispenser1 = dispenser('热水', '冷水', '客厅');
    console.log(dispenser1);
    // 创建三个怪物对象，并且输出怪物的名字、性别、等级，怪物执行攻击方法，输出“水球术”
    function skill() {
      alert(this._skill);
    }
    function createMonster(name, sex, level, skill01) {
      var obj = new Object();
      obj.name = name;
      obj.sex = sex;
      obj.level = level;
      obj._skill = skill01;
      obj.skill = skill;
      return obj;
    }
    var monster1 = createMonster('怪物1', '男', '1', '水球术');
    var monster2 = createMonster('怪物2', '女', '2', '水球术');
    var monster3 = createMonster('怪物3', '男', '3', '水球术');
    console.log(monster3);
    console.log(monster1);
    console.log(monster2);
    monster1.skill();
    // 创建一个动漫对象，输出动漫的人物，爱好，性别，能力
    function anime(name, hobby, sex, skill1) {
      var obj = new Object();
      obj.name = name;
      obj.hobby = hobby;
      obj.sex = sex;
      obj.skill1 = skill1;
      return obj;
    }
    var anime1 = anime('哪吒', '玩耍', '男', '乾坤圈');
    console.log(anime1);
    /*  创建一个电脑对象，有颜色(color)、重量(weight)、品牌(brand)、型号(type)等属性，有看电影(watch)、听音乐(listen)、打游戏(play)和敲代码(coding)等方法。
   *颜色、重量、品牌、型号是属性；看电影、听音乐、打游戏、敲代码是方法，在调用方法时分别打印 “我在看电影/听音乐/打游戏/敲代码”
   *方法保存在原型对象中 */
    /* function watch() {
      console.log('我在看电影');
    }
    function listen() {
      console.log('我在听音乐');
    }
    function play() {
      console.log('我在打游戏');
    }
    function coding() {
      console.log('我在敲代码');
    } */
    CreateComputer.prototype.watch = function () {
      console.log('我在看电影');
    }
    CreateComputer.prototype.listen = function () {
      console.log('我在听音乐');
    }
    CreateComputer.prototype.play = function () {
      console.log('我在打游戏');

    }
    CreateComputer.prototype.coding = function () {
      console.log('我在敲代码');
    }
    function CreateComputer(color, weight, brand, type) {
      this.color = color;
      this.weight = weight;
      this.brand = brand;
      this.type = type;
      /*  this.watch = watch;
       this.listen = listen;
       this.play = play;
       this.coding = coding; */
    }
    var computer = new CreateComputer('玫瑰金', '2kg', '联想', '最新型号');
    console.log(computer);
    computer.watch();
    computer.listen();
    computer.play();
    computer.coding();
    /*  
      *以上是某班级一次考试的成绩表。请计算每个学生总成绩，并按总成绩排名。统计各单科成绩第一名，输出其成绩与学号。
      *并根据总成绩计算每个学生成绩备注保存在 Comment 中，60分以上为D，70分以上为C，80分以上为B，90分以上为A，60分以下为E 
    */
    var data = [
      { StudentNumber: 2022003, Chinese: 86, Math: 64, English: 80, TotalScore: "", Comment: "" },
      { StudentNumber: 2022004, Chinese: 78, Math: 99, English: 91, TotalScore: "", Comment: "" },
      { StudentNumber: 2022005, Chinese: 107.5, Math: 97, English: 70, TotalScore: "", Comment: "" },
      { StudentNumber: 2022006, Chinese: 112, Math: 61, English: 92, TotalScore: "", Comment: "" },
      { StudentNumber: 2022007, Chinese: 101, Math: 79, English: 104, TotalScore: "", Comment: "" },
      { StudentNumber: 2022008, Chinese: 71, Math: 72, English: 105, TotalScore: "", Comment: "" },
      { StudentNumber: 2022009, Chinese: 56, Math: 68, English: 61, TotalScore: "", Comment: "" },
      { StudentNumber: 2022010, Chinese: 98, Math: 83, English: 77, TotalScore: "", Comment: "" }
    ];
    /* for (let i in data) {
      data[i].TotalScore = data[i].Chinese + data[i].Math + data[i].English;
    }
    for (let j in data) {
      var avg = data[j].TotalScore / 3;
      if (avg >= 90) {
        data[j].Comment = 'A';
      } else if (avg >= 80) {
        data[j].Comment = 'B';
      } else if (avg >= 70) {
        data[j].Comment = 'C';
      } else if (avg >= 60) {
        data[j].Comment = 'D';
      } else if (avg < 60) {
        data[j].Comment = 'E';
      }
    }
    var arr = { Chinese: '语文', Math: '数学', English: '英语' }
    function ScoreEvery(data, subject) {
      this.data = data;
      this.subject = subject;
    }
    ScoreEvery.prototype.out = function () {
      var _data = this.data;
      var _subject = this.subject;
      var _arr = this.arr;
      _data.sort(function (a, b) {
        return b[_subject] - a[_subject];
      })
      console.log(arr[_subject] + '最高成绩为：' + _data[0][_subject] + '分 ' + '学号为：' + _data[0].StudentNumber)
    }
    var chineseScore = new ScoreEvery(data, 'Chinese');
    var mathScore = new ScoreEvery(data, 'Math');
    var englishScore = new ScoreEvery(data, 'English');
    chineseScore.out();
    mathScore.out();
    englishScore.out(); */

    //2构造函数
    function ScoreHandling(data) {
      this.data = data;
    }
    ScoreHandling.prototype.scoreSort = function (data, subject, subjectText) {
      data.sort(function (a, b) {
        return b[subject] - a[subject];
      })
      console.log(subjectText + '第一名的学号为：' + data[0].StudentNumber + '; 成绩为：' + data[0][subject] + '分 ');
    }
    ScoreHandling.prototype.outputScore = function () {
      let _data = this.data;
      for (let i of data) {
        i.TotalScore = i.Chinese + i.Math + i.English;
        var avg = (i.TotalScore / 3).toFixed(1);
        if (avg >= 90) {
          i.Comment = 'A';
        } else if (avg >= 80) {
          i.Comment = 'B';
        } else if (avg >= 70) {
          i.Comment = 'C';
        } else if (avg >= 60) {
          i.Comment = 'D';
        } else {
          i.Comment = 'E';
        }
      }
      console.log(_data);
      this.scoreSort(_data, 'TotalScore', '总成绩');
      this.scoreSort(_data, 'Chinese', '语文');
      this.scoreSort(_data, 'Math', '数学');
      this.scoreSort(_data, 'English', '英语');

      /* 可以分步计算总分、语文、数学、英语最大成绩
      _data.sort(function(a,b){
        return b.TotalScore-a.TotalScore;
      })
      console.log('第一名的学号为：' + _data[0].StudentNumber+'; 成绩为：'+ _data[0].TotalScore + '分 ');
    */
    }

    var score01 = new ScoreHandling(data);
    score01.outputScore();
  </script>
</body>

</html>